package com.ruoyi.common.utils.kiam;


import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class Utils {
    private static final String ENCODING = "UTF-8";
    private static HostnameVerifier ignoreHostnameVerifier = new HostnameVerifier() {
        public boolean verify(String arg0, SSLSession arg1) {
            return true;
        }
    };
    private static TrustManager ignoreCertificationTrustManger = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };

    public Utils() {
    }

    public static String sendUrlRequest(String urlStr, String content, String requestMethod) throws Exception {
        System.out.println("发送请求" + urlStr);
        System.out.println("请求content" + content);
        HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier);
        HttpURLConnection connection = null;
        String tempStr = null;

        try {
            URL url = new URL(urlStr);
            connection = (HttpURLConnection)url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setRequestProperty("User-Agent", "directclient");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            if (connection instanceof HttpsURLConnection) {
                TrustManager[] tm = new TrustManager[]{ignoreCertificationTrustManger};
                SSLContext sslContext = SSLContext.getInstance("SSL");
                sslContext.init((KeyManager[])null, tm, new SecureRandom());
                SSLSocketFactory ssf = sslContext.getSocketFactory();
                ((HttpsURLConnection)connection).setSSLSocketFactory(ssf);
            }

            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.writeBytes(content);
            out.flush();
            out.close();
            StringBuffer bankXmlBuffer = new StringBuffer();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));

            String inputLine;
            while((inputLine = in.readLine()) != null) {
                bankXmlBuffer.append(inputLine);
            }

            in.close();
            tempStr = bankXmlBuffer.toString();
        } catch (Exception var13) {
            System.out.println("发送请求出现异常！");
            var13.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }

        }

        return tempStr;
    }

    public static String sendUrlRequestWithGet(String urlStr) throws Exception {
        System.out.println("发送请求" + urlStr);
        HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier);
        HttpURLConnection connection = null;
        String tempStr = null;

        try {
            URL url = new URL(urlStr);
            connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setRequestProperty("User-Agent", "directclient");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            if (connection instanceof HttpsURLConnection) {
                TrustManager[] tm = new TrustManager[]{ignoreCertificationTrustManger};
                SSLContext sslContext = SSLContext.getInstance("SSL");
                sslContext.init((KeyManager[])null, tm, new SecureRandom());
                SSLSocketFactory ssf = sslContext.getSocketFactory();
                ((HttpsURLConnection)connection).setSSLSocketFactory(ssf);
            }

            StringBuffer bankXmlBuffer = new StringBuffer();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));

            String inputLine;
            while((inputLine = in.readLine()) != null) {
                bankXmlBuffer.append(inputLine);
            }

            in.close();
            tempStr = bankXmlBuffer.toString();
        } catch (Exception var10) {
            System.out.println("发送请求出现异常！");
            var10.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }

        }

        return tempStr;
    }
}
